# Maintainer: Daurnimator <daurnimator@archlinux.org>

pkgname=('lua-cqueues' 'lua51-cqueues' 'lua52-cqueues' 'lua53-cqueues')
pkgver=20200726
pkgrel=4
arch=('loong64' 'x86_64')
pkgdesc='Continuation Queues: Embeddable asynchronous networking, threading, and notification framework'
url='http://25thandclement.com/~william/projects/cqueues.html'
license=('MIT')
makedepends=('luarocks' 'lua' 'lua51' 'lua52' 'lua53')
depends=('openssl')
source=("$pkgname-$pkgver.tar.gz::https://github.com/wahern/cqueues/archive/rel-$pkgver.tar.gz"
	    "https://luarocks.org/manifests/daurnimator/cqueues-$pkgver.5"{1,2,3,4}"-0.rockspec")
sha256sums=('9e112edd246da5cfca264314b70325a0b63665cb87a00e45ee3ae4f194000d52'
            'bbebe2285799895060c5acb565f5a4443d653f4a0d7a76ac8b49b272f8c6edf8'
            '1742fa4b2c083625af3d3b69706c1d6c99cfd8afbbca2cc03445350e78c25370'
            'f5bae8418551341d8c17ec302af27269098752e8fbda5f7bd1e610f2cb39fe2e'
            'd78cba24427812a711d10a05b410b2afb6a9d4002d51dd2a4f4c55ba1a50d48c')

build() {
	cd "cqueues-rel-$pkgver"
	for v in 5.1 5.2 5.3 5.4; do
		mkdir -p "$v/"
		# Use `-ffile-prefix-map` to avoid embedding srcdir into binary
		# Packed rocks with make backend don't work?
		# Workaround: don't use --pack-binary-rock; instead make into a temporary tree and then pack
		luarocks make --lua-version="$v" --tree="../luarocks-temp-$v" --deps-mode=none \
			CFLAGS="$CPPFLAGS $CFLAGS -fPIC -ffile-prefix-map=$(pwd)=" \
			LIBFLAG="$LDFLAGS -shared" \
			../cqueues-"$pkgver".5${v#5.}-0.rockspec
		luarocks pack --lua-version="$v" --tree="../luarocks-temp-$v" cqueues
		mv cqueues-"$pkgver".*.rock "$v/"
	done
}

package_lua-cqueues() {
	pkgdesc="$pkgdesc for Lua 5.4"
	optdepends=('lua-luaossl: TLS support')

	cd "cqueues-rel-$pkgver"
	luarocks install --lua-version=5.4 --tree="$pkgdir/usr/" --deps-mode=none 5.4/*.rock --no-manifest
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_lua51-cqueues() {
	pkgdesc="$pkgdesc for Lua 5.1"
	optdepends=('lua51-luaossl: TLS support')

	cd "cqueues-rel-$pkgver"
	luarocks install --lua-version=5.1 --tree="$pkgdir/usr/" --deps-mode=none 5.1/*.rock --no-manifest
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_lua52-cqueues() {
	pkgdesc="$pkgdesc for Lua 5.2"
	optdepends=('lua52-luaossl: TLS support')

	cd "cqueues-rel-$pkgver"
	luarocks install --lua-version=5.2 --tree="$pkgdir/usr/" --deps-mode=none 5.2/*.rock --no-manifest
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_lua53-cqueues() {
	pkgdesc="$pkgdesc for Lua 5.3"
	optdepends=('lua53-luaossl: TLS support')

	cd "cqueues-rel-$pkgver"
	luarocks install --lua-version=5.3 --tree="$pkgdir/usr/" --deps-mode=none 5.3/*.rock --no-manifest
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
